/**
 * Created with JetBrains PhpStorm.
 * User: kk
 * Date: 13-8-28
 * Time: 下午4:44
 */ 
$(function () {
	//屏蔽热键 F5
	$(window).keydown(function(e){
		if(e.keyCode==116){
			document.location.reload();
			return false;
		}
	});
	//兼容ie
	document.onkeydown=function(){
		if(window.event){
			if(event.keyCode==116){
				window.event.keyCode=0;  
				window.event.returnValue=false;
				document.location.reload();
			}
		}		
	}
    //操作确认
    $('.jsconfirm').click(function(){
        var self = $(this) ,sText = self.attr('title');
		parent.jsconfirm(sText+"？" ,
			function(){
				$.getJSON(self.attr('url'),function(data){
					if(data.err==0){
						SendWebMsg('success', data.msg);
                        window.location.reload();
						//ts('删除成功');
					}else{
					    SendWebMsg('error', data.msg);   
						//parent.jsshow('删除失败','','error');
					}
				})
			} ,'info');
		return false;
    });
	//删除单条数据
	$('.delete').click(function(){
		var self = $(this);
		parent.jsconfirm("确定要删除吗？" ,
			function(){
				$.getJSON(self.attr('url'),function(data){
					if(data.err==0){
						data.url ? window.location.href=data.url : $(self).parent().parent().remove();                        
						//ts('删除成功');
					}else{
						parent.jsshow('删除失败','','error');
					}
				})
			} ,'info');
		return false;
	 });
	 //批量删除
	$('.deleteall').click(function(){
		var checked=$('.ids:checked') ,self = $(this);
		
		if(checked.length>0){
			parent.jsconfirm('确定要批量删除数据吗？' ,
				function(){
					var arr=new Array();
					$(".ids:checked").each(function(){
						arr.push($(this).val());
					});
					value_str=arr.join('|');
					$.post(self.attr('url'),{ids:value_str},function(data){
						var myObject = eval('(' + data + ')');
						if(myObject.err==0){
							checked.parent().parent().remove();
							//ts('删除成功');
						}else{
							parent.jsshow('删除失败','','error');
						}
					})
				} ,'info');
				
		}else{
			parent.jsshow('请选择要操作的选项','','error');
		}
		return false;
	 });
	//表格全选
	$(".btn_all").click(function(){
		if($(this).attr('checked')=='checked'){
			$("tbody input[type='checkbox']:enabled").attr("checked",true);
		}else{
			$("tbody input[type='checkbox']:enabled").attr("checked",false);
		}
	});
	$('.ids').click(function(){
		if($('.ids:checked').size()>0){
			$('.btn_all').attr('checked',true);
		}else{
			$('.btn_all').attr('checked',false);
		}
	});
	//
});

/*页面提示 扩展函数*/
function SendWebMsg(check,msg,url,reloadifame){
	var parents=$(window.parent.document).find('#iframe_load');
    if(!arguments[4]) reloadifame = "other";
	parents.hide();
	parents.find('span').html('请等待，加载中…');
	parent.addInfo(check,msg);
	if(url && reloadifame=='other' ){
		window.location.href=url;
	} else if ( url && reloadifame=='editpersonpass' ) {
	   window.parent.location.href = url;  
	}
}
//ajax批量迁移列表页面的数据至分类、品牌等
function MoveAll(url, moveall_id) {
	var checked = $('.ids:checked');
    var action_type = $('#action_type').val(); // 操作类型
    var moveall_id = $('#' + moveall_id).val(); // 分类、品牌等下拉列表的ID
    if (checked.length > 0) {
        if (moveall_id != 0) {
            if (confirm('确定要批量迁移数据吗？')) {
                var arr = new Array();
                $(".ids:checked").each(function() {
                    arr.push($(this).val());
                });
                value_str = arr.join(',');
                $.post(url,{ids:value_str, action_type:action_type, moveall_id:moveall_id},function(data) {
                    var myObject = eval('(' + data + ')');
                    if (myObject.err == 0) {
                        SendWebMsg('success', myObject.msg);
                        window.location.reload();
                    }else{
                        SendWebMsg('error', myObject.msg);
                    }
                })
            }
        } else {
            SendWebMsg('cue', '请选择分类');
            return false;
        }
    } else {
        SendWebMsg('cue', '请选择要操作的选项');
        return false;
    }
}

//弹出商品选择框
function alert_search_goods(){
    //SearchGoodsUrl = SearchGoodsUrl+'?cat_id='+$('#cat_id').val()+'&keywords='+$('#keywords').val();
	art.dialog.open(SearchGoodsUrl,{
		id: 'ck_search_goods',
	    title:'商品检索',
	    width:'800px',
	    height:'600px',
	    padding:'0px',
	    ok: function () {
	    	var iframe = $(this.iframe.contentWindow.document);
	    	select_goods(iframe);
	        return true;
	    },
	    cancelVal: '关闭',
	    cancel: true,
	    lock: true
	 });
}
//表单中的input用方向键控制焦点
function upDownLfetRight(selector,self,key,line){
    var inputs=$(selector);
    var i = inputs.index(self);
    var inputLength=inputs.length;
    switch(key) 
    { 
        case 37: //左
            if(i>0) var curInput=inputs.get(i-1); 
            break; 
        case 38: //上
            if(i-line>=0) var curInput=inputs.get(i-line); 
            break; 
        case 39: //右
            if(i<inputLength-1) var curInput=inputs.get(i+1);
            break; 
        case 40: //下
            if(i+line <inputLength) var curInput=inputs.get(i+line); 
            break; 
    } 
    if(curInput) curInput.select();
}
//格式化价格
function formatNumber(value, num){
  //value = Math.abs(parseFloat(value));
  value = parseFloat(value);
  var a,b,c,i;
  a=value.toString();
  b=a.indexOf('.');
  c=a.length;
  if (num==0){
    if (b!=- 1){
      a=a.substring(0, b);
    }
  }
  else{
    if (b==- 1){
      a=a+".";
      for (i=1;i<=num;i++ ){
        a=a + "0";
      }
    }
    else{
      a=a.substring(0, b + num + 1);
      for(i=c;i<=b+num;i++ ){
        a=a + "0";
      }
    }
  }
  return a;
}